package com.gw.vip.center.zib;

import com.gw.vip.center.dto.MailRegisterDto;
import com.gw.vip.center.jwt.JwtUtil;
import com.gw.vip.center.jwt.TokenInfo;
import com.gw.vip.center.po.UserInfo;
import com.gw.vip.center.po.UserLogin;
import com.gw.vip.center.service.PassWordService;
import com.gw.vip.center.service.dao_service.UserInfoService;
import com.gw.vip.center.service.UserLoginService;
import com.gw.vip.center.vo.MailRegisterVo;
import com.gw.vip.common.constants.VipConstants;
import com.gw.vip.common.exception.ExceptionEnums;
import com.gw.vip.common.exception.ServiceException;
import com.gw.vip.common.util.Snowflake;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author GW
 * @description
 * @since 2022-04-17
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RegisterBiz {

    private final Snowflake snowflake;
    private final UserInfoService userInfoService;
    private final UserLoginService userLoginService;
    private final PassWordService passWordService;

    /**
     * 邮箱注册
     * @param dto
     * @return
     */
    public MailRegisterVo mailRegister(MailRegisterDto dto) {
        //检查邮箱是否重复注册
        UserInfo userInfoMail = userInfoService.getByMail(dto.getMailAddress());
        if (userInfoMail!=null){
            log.error("该邮箱已经被注册！邮箱【{}】", dto.getMailAddress());
            throw new ServiceException(ExceptionEnums.USER_EXIST, "该邮箱已经被注册！");
        }

        //密码解密
        String passWord = passWordService.decrypt(dto.getEncPassWork(), dto.getChannelId(), dto.getUnionId());

        //保存用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setId(snowflake.nextStringId());
        userInfo.setNickName(dto.getNickName());
        userInfo.setMail(dto.getMailAddress());
        userInfo.setPassword(passWord);
        userInfo.setCrtTime(LocalDateTime.now());
        userInfo.setCrtName(VipConstants.SYS);
        userInfo.setUpdTime(userInfo.getCrtTime());
        userInfo.setUpdName(VipConstants.SYS);
        if (!userInfoService.save(userInfo)){
            log.error("保存用户失败！");
            throw new ServiceException(ExceptionEnums.SYSTEM_ERROR, "保存用户失败!");
        }

        UserLogin userLogin = userLoginService.getByBase(dto.getChannelId(), dto.getUnionId());
        if (userLogin==null){
            //新增登陆信息
            userLogin = new UserLogin();
            userLogin.setId(snowflake.nextId());
            userLogin.setChannelId(dto.getChannelId());
            userLogin.setUnionId(dto.getUnionId());
            userLogin.setUserId(userInfo.getId());
            userLogin.setCrtTime(userInfo.getCrtTime());
            userLogin.setCrtName(VipConstants.SYS);
            userLogin.setUpdTime(userLogin.getCrtTime());
            userLogin.setUpdName(VipConstants.SYS);
            userLoginService.save(userLogin);
        }else{
            //更新userLogin信息
            userLogin.setUserId(userInfo.getId());
            userLogin.setUpdTime(userInfo.getCrtTime());
            userLoginService.updateById(userLogin);
        }

        //token信息
        TokenInfo tokenInfo = new TokenInfo();
        tokenInfo.setUserId(userInfo.getId());
        String token = JwtUtil.token(tokenInfo);

        MailRegisterVo vo = new MailRegisterVo();
        vo.setUserInfo(userInfo);
        vo.setToken(token);
        return vo;
    }
}
